package com.controller;

import com.domain.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.CommentMapper;
import com.service.CommentService;
import com.utils.MyUtils;
import com.vdurmont.emoji.EmojiParser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
//import java.util.List;

@Controller
public class BlogCommentController {
    @Autowired
    CommentService commentService;

    @PostMapping(value = "comments/publish")
    @ResponseBody
    public ArticleResponseData publishComment(HttpServletRequest request, @RequestParam Integer aid,@RequestParam String text){
        text = MyUtils.cleanXSS(text);
        text = EmojiParser.parseToAliases(text);

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        BlogComment comments = new BlogComment();
        comments.setArticleId(aid);
        comments.setIp(request.getRemoteAddr());
        comments.setCreated(new Date());
        comments.setAuthor(user.getUserName());
        comments.setContent(text);
        try{
            commentService.pushComment(comments);
            logger.info("发布评论成功，对应文章id" +aid);
            return ArticleResponseData.ok();
        }catch (Exception e){
            logger.error("发布评论失败，对应文章id" +aid +";错误描述：" +e.getMessage());
            return ArticleResponseData.fail();
        }
    }
    @Autowired
    CommentMapper commentMapper;

//    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
//    @PostMapping("admin/comment/delete")
//    @ResponseBody
//    public ArticleResponseData deleteComment(int id){
//        try{
//            commentMapper.deleteComment(id);
//            logger.info("文章删除成功");
//            return ArticleResponseData.ok();
//        }catch (Exception e){
//            logger.error("文章删除失败，错误信息："+e.getMessage());
//            return ArticleResponseData.fail();
//        }
//    }
//    @GetMapping(value = "/admin/comments")
//    public String index(@RequestParam(value = "page",defaultValue = "1") int page,
//                        @RequestParam(value = "count",defaultValue = "10")int count,
//                        HttpServletRequest request){
//        PageHelper.startPage(page,count);
//        List<BlogComment> commentList = commentMapper.selectNewComment();
//        PageInfo<BlogComment>pageInfo = new PageInfo<>(commentList);
//        request.setAttribute("comments",pageInfo);
//        return "back/comment_list";
//    }
//    @Autowired
//    CommentMapper commentMapper;

    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
    @PostMapping(value = "/admin/comment/delete")
    @ResponseBody
    public ArticleResponseData deleteComment(int id){
        try{
            commentMapper.deleteComment(id);
            logger.info("文章删除成功");
            return ArticleResponseData.ok();
        }catch (Exception e){
            logger.error("文章删除失败，错误信息："+e.getMessage());
            return ArticleResponseData.fail();
        }
    }
    @GetMapping(value = "/admin/comments")
    public String index(@RequestParam(value = "page",defaultValue = "1")int page,
                        @RequestParam(value = "count",defaultValue = "10")int count,
                        HttpServletRequest request){
        PageHelper.startPage(page,count);
        List<BlogComment> commentList = commentMapper.selectComment();
        PageInfo<BlogComment>pageInfo = new PageInfo<>(commentList);
        request.setAttribute("comments",pageInfo);
        return "back/comment_list";
    }


}
